/**
 * 自定义数据校验
 * */
const Validation = {};
Validation.install = function (Vue, options) {
  Vue.prototype.$commonRules = function (item) {
    const trigger = item.trigger ?? 'blur'; // 默认bluer,如果select传 change
    const dic = {
      required: { required: true, message: item.msg ?? '该输入项为必填项!', trigger }
    };

    // 按校验字典生成规划
    let rules = [];
    if (item.rule) {
      const arr = item.rule.split('|');
      arr.map(x => {
        rules.push(dic[x]);
      });
    }
    if (item.len) {
      rules.push({ min: 0, max: item.len, message: '最多输入' + item.len + '个字符!', trigger });
    }
    if (item.min && item.max) {
      rules.push({ min: item.min, max: item.max, message: '字符长度在' + item.min + '至' + item.max + '之间!', trigger });
    }
    // 自定义正则验证
    if (item.regexp) {
      rules.push({ pattern: item.regexp, message: item.msg, trigger });
    }
    return rules;
  };
};

export default Validation;
